package com.hnuit.service;

import com.hnuit.Utils.MybatisUtils;
import com.hnuit.dao.CoursesDao;
import com.hnuit.dao.UserDao;
import com.hnuit.pojo.Courses;

import com.hnuit.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CoursesDemo {
    @Test
    public void findAllCourses() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        CoursesDao dao=session.getMapper(CoursesDao.class);
        List<Courses> all=dao.findAllCourses();
        System.out.println(all);
        session.close();
    }
    @Test
    public void findCoursesById() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        CoursesDao dao=session.getMapper(CoursesDao.class);
        Courses courses=dao.findCoursesById(1);
        System.out.println(courses);
        session.close();
    }
    @Test//删除操作
    public void deleteCoursesById() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        CoursesDao dao=session.getMapper(CoursesDao.class);
        Courses courses=dao.deleteCoursesById(3);
        System.out.println("已删除");
        session.commit();
        session.close();
    }
    @Test//增加操作
    public void addUser() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        CoursesDao dao=session.getMapper(CoursesDao.class);
        Courses courses=new Courses();
        courses.setId(3);
        courses.setName("物理");
        courses.setDescription("不错");
        courses.setInstructor_id(3);
        dao.addCourses(courses);
        session.commit();
        session.close();
    }
    @Test//更新操作
    public void updateUser() throws IOException{
        SqlSession session=MybatisUtils.getSession();
        CoursesDao dao=session.getMapper(CoursesDao.class);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",2);
        map.put("name","化学");
        map.put("description","可以");
        map.put("instructor_id","3");
        dao.updateCourses(map);
        session.commit();
        session.close();
    }
}
